void SB_SendPacket() 
{
    if (SB_CommandMode == B01) {
     SB_RedCommand = 127;
     SB_GreenCommand = 110;
     SB_BlueCommand = 110;
    }
 
    SPDR = SB_CommandMode << 6 | SB_BlueCommand>>4;
    while(!(SPSR & (1<<SPIF)));
    SPDR = SB_BlueCommand<<4 | SB_RedCommand>>6;
    while(!(SPSR & (1<<SPIF)));
    SPDR = SB_RedCommand << 2 | SB_GreenCommand>>8;
    while(!(SPSR & (1<<SPIF)));
    SPDR = SB_GreenCommand;
    while(!(SPSR & (1<<SPIF)));
 
}

void SB_Latch()
{
 
  delayMicroseconds(1);
  LATPORT += (1 << LATPIN);
  delayMicroseconds(1);
  LATPORT &= ~(1 << LATPIN);
}

void WriteLEDArray()
    {
 
    SB_CommandMode = B00;
    for (int h = 0;h<NumLEDs;h++) {
	  SB_RedCommand = LEDChannels[h][0];
	  SB_GreenCommand = LEDChannels[h][1];
	  SB_BlueCommand = LEDChannels[h][2];
	  SB_SendPacket();
    }
 
    delayMicroseconds(15);
    digitalWrite(latchpin,HIGH);
    delayMicroseconds(15);
    digitalWrite(latchpin,LOW);
 
    SB_CommandMode = B01;
    for (int z = 0; z < NumLEDs; z++) SB_SendPacket();
    delayMicroseconds(15);
    digitalWrite(latchpin,HIGH);
    delayMicroseconds(15);
    digitalWrite(latchpin,LOW); 
}



int gammatable[]={0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,2,2,2,2,2,3,3,3,4,4,4,5,5,6,6,6,7,7,8,8,9,10,10,11,12,12,13,14,14,15,16,17,18,19,19,20,21,22,23,24,26,27,28,29,30,31,33,34,35,36,38,39,41,42,44,45,47,48,50,52,53,55,57,58,60,62,64,66,68,70,72,74,76,78,80,82,85,87,89,92,94,96,99,101,104,106,109,112,114,117,120,123,125,128,131,134,137,140,143,146,149,153,156,159,162,166,169,172,176,179,183,187,190,194,198,201,205,209,213,217,221,225,229,233,237,241,246,250,254,258,263,267,272,276,281,286,290,295,300,305,310,314,319,324,329,335,340,345,350,355,361,366,372,377,383,388,394,400,405,411,417,423,429,435,441,447,453,459,465,472,478,484,491,497,504,510,517,524,530,537,544,551,558,565,572,579,586,593,601,608,615,623,630,638,645,653,661,668,676,684,692,700,708,716,724,732,740,749,757,765,774,782,791,800,808,817,826,835,844,853,862,871,880,889,898,908,917,926,936,945,955,965,974,984,994,1004,1014,1023};

 const int numReadings = 1;
 int bluereadings[numReadings];      
 int blueindex = 0;                 
 int bluetotal = 0;                  
 int redreadings[numReadings];     
 int redindex = 0;                 
 int redtotal = 0;                 
 int greenreadings[numReadings];    
 int greenindex = 0;               
 int greentotal = 0;                


void outputpwm(int curchannel)
{
  int blue=gammatable[incomingatmo[(channelorder[curchannel]*3)+3]];
  int red=gammatable[incomingatmo[(channelorder[curchannel]*3)+1]];
  int green=gammatable[incomingatmo[(channelorder[curchannel]*3)+2]];
  
   bluetotal= bluetotal - bluereadings[blueindex];
  redtotal= redtotal - redreadings[redindex]; 
  greentotal= greentotal - greenreadings[greenindex]; 
 
   bluereadings[blueindex] =  blue;
   redreadings[redindex] =  red;
   greenreadings[greenindex] =  green;
 
   bluetotal= bluetotal + bluereadings[blueindex];  
   redtotal= redtotal + redreadings[redindex]; 
   greentotal= greentotal + greenreadings[greenindex];  
   blueindex = blueindex + 1;  
   redindex = redindex + 1;
   greenindex = greenindex + 1;

   if (blueindex >= numReadings)              
     blueindex = 0;   
    if (redindex >= numReadings)              
       redindex = 0;  
    if (greenindex >= numReadings)              
      greenindex = 0;       
  
    int average = redtotal / numReadings; 
     LEDChannels[curchannel][0]=average;
   average = greentotal / numReadings; 
  LEDChannels[curchannel][1]=average;
  
   average = bluetotal / numReadings; 
   LEDChannels[curchannel][2]=average;  
  }


